package com.monitor.service.parser.impl;

import com.monitor.model.ServerStatus;
import com.monitor.service.parser.MetricParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 系统运行时间指标解析器
 * 负责解析系统运行时间信息
 */
public class UptimeMetricParser implements MetricParser {
    private static final Logger logger = LoggerFactory.getLogger(UptimeMetricParser.class);
    private static final Pattern uptimePattern = Pattern.compile("(\\d+) days, (\\d+):(\\d+)");

    @Override
    public void parse(String commandOutput, ServerStatus status) {
        try {
            String uptime = commandOutput.trim();
            Matcher matcher = uptimePattern.matcher(uptime);
            
            if (matcher.find()) {
                int days = Integer.parseInt(matcher.group(1));
                int hours = Integer.parseInt(matcher.group(2));
                int minutes = Integer.parseInt(matcher.group(3));
                
                status.setUptime(String.format("%d天%d小时%d分钟", days, hours, minutes));
            } else {
                status.setUptime(uptime);
            }
        } catch (Exception e) {
            logger.error("解析系统运行时间失败: {}", e.getMessage());
            status.setUptime("解析错误");
        }
    }
}